package com.api.shopapi.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @author maochuan
 */
public class Util {
    /**
     * 计算时间 得到天数
     *
     * @param startTime ： 开始时间
     * @param endTime   ： 结束时间
     * @return
     */
    public static int caculateTotalTime(String startTime, String endTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        Date date = null;
        Long l = 0L;
        try {
            date = formatter.parse(startTime);
            long ts = date.getTime();
            date1 = formatter.parse(endTime);
            long ts1 = date1.getTime();

            l = (ts1 - ts) / (1000 * 60 * 60 * 24);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return l.intValue();
    }

    /**
     * 比较两个月份大小
     * @param nowtime
     * @param endtime
     * @return
     */
    public static int size(String nowtime,String endtime){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            if(sdf.parse(nowtime).getTime()>sdf.parse(endtime).getTime()){//转成long类型比较

               // System.out.println("当前时间大于到期时间");
                return 1;
            }else if(sdf.parse(nowtime).getTime()<sdf.parse(endtime).getTime()){
               // System.out.println("当前时间小于到期时间");
                return 2;
            }else if(sdf.parse(nowtime).getTime()==sdf.parse(endtime).getTime()){
               // System.out.println("当前时间等于到期时间");
                return 3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 4;
    }

    /**
     * 比较两个日期大小
     * @param nowtime
     * @param endtime
     * @return
     */
    public static int dateSize(String nowtime,String endtime){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if(sdf.parse(nowtime).getTime()>sdf.parse(endtime).getTime()){//转成long类型比较

                // System.out.println("当前时间大于到期时间");
                return 1;
            }else if(sdf.parse(nowtime).getTime()<sdf.parse(endtime).getTime()){
                // System.out.println("当前时间小于到期时间");
                return 2;
            }else if(sdf.parse(nowtime).getTime()==sdf.parse(endtime).getTime()){
                // System.out.println("当前时间等于到期时间");
                return 3;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 4;
    }
    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate=sdf.parse(sdf.format(smdate));
            bdate=sdf.parse(sdf.format(bdate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
    /**
     * 计算两个日期之间相差的月数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int monthBetween(Date smdate,Date bdate){
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(smdate);
        aft.setTime(bdate);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return month+result;
    }


    /**
     * 日期加一天
     * @param time
     * @return
     */
    public static String timeAddDay(String time,int day){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = null;
        try {
            time1 = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(time1);
        calendar.add(calendar.DATE, day); //把日期往后增加一天,整数  往后推,负数往前移动
        time1 = calendar.getTime(); //这个时间就是日期往后推一天的结果

        String time2 = sdf.format(time1);
        return time2;
    }
    public static Date getFirstDayOfMonthByDate(Date date_str) {
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        cale.setTime(date_str);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String firstDayOfMonth = formatter.format(cale.getTime()); // 当月第一天 2019-02-01
        Date parse = null;
        try {
            parse = formatter.parse(firstDayOfMonth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;

    }

    public static String getFirstDayOfMonthByDateStr(String date_str) throws ParseException {
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = formatter.parse(date_str);
        cale.setTime(parse);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return formatter.format(cale.getTime()); // 当月第一天 2019-02-01
    }

    public static String getLastDayOfMonthByDateStr(String date_str) throws ParseException {
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = formatter.parse(date_str);
        cale.setTime(parse);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return formatter.format(cale.getTime()); // 当月第一天 2019-02-01
    }

    public static Date getLastDayOfMonthByDate(Date date_str) {
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        cale.setTime(date_str);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDayOfMonth = formatter.format(cale.getTime()); // 当月最后一天 2019-02-28
        Date parse = null;
        try {
            parse = formatter.parse(lastDayOfMonth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;

    }
    //获取当月总天数
    public static int getDays(){
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    //获取当月总天数
    public static int getDaysByDate(Date date_str){
        Calendar a = Calendar.getInstance();
        a.setTime(date_str);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    //获取当前已过天数
    public static int getUseDays(){
        Calendar a = Calendar.getInstance();
        int day = a.get(Calendar.DAY_OF_MONTH);    //获取当前天数
        return day;
    }

    public static String getStringLastDayOfMonth(){
        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获得实体类
        Calendar ca = Calendar.getInstance();
        //设置最后一天
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //最后一天格式化
        String lastDay = format.format(ca.getTime());
        return lastDay;
    }

    public static Date getLastDayOfMonth(){
        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获得实体类
        Calendar ca = Calendar.getInstance();
        //设置最后一天
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //最后一天格式化
        String lastDay = format.format(ca.getTime());
        Date parse = null;
        try {
            parse = format.parse(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    //获取当天时间并转为Date格式
    public static Date today(){
        LocalDate today = LocalDate.now();
        ZoneId zone = ZoneId.systemDefault();
        Date day = Date.from(today.atStartOfDay().atZone(zone).toInstant());
        return day;
    }

    //获取一年后的日期并转为Date格式
    public static Date nextYear(){
        LocalDate today = LocalDate.now();
        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        ZoneId zone = ZoneId.systemDefault();
        Date day = Date.from(nextYear.atStartOfDay().atZone(zone).toInstant());
        return day;
    }

    //计算一年前或一年后的日期
    public void minusDate(){
        LocalDate today = LocalDate.now();
        LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
        System.out.println("Date before 1 year : " + previousYear);

        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("Date after 1 year : " + nextYear);
    }

    //两个日期时间相差几天,几小du时,几分钟
    public static List<Long> dateDiff(String startTime, String endTime, String format) throws ParseException {
        //按照dao传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000*24*60*60;//一天的毫秒数
        long nh = 1000*60*60;//一小时的毫秒数
        long nm = 1000*60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数long diff;try {
        //获得两个时间的毫秒时间差异
        long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        List<Long> list=new ArrayList();
        list.add(diff/nd);//计算差多少天
        list.add(diff%nd/nh);//计算差多少小时
        list.add(diff%nd%nh/nm);//计算差多少分钟
        list.add(diff%nd%nh%nm/ns);//计算差多少秒//输出结果
        return list;
    }

    //
    public static String getLastDayOfMonthType(int type,Date date){
        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDay=format.format(cale.getTime());
        return type==1?lastDay:lastDay+" 23:59:59";
    }
    //
    public static String getFirstDayOfMonthType(int type,Date date){
        //设置时间格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String lastDay=format.format(cale.getTime());
        return type==1?lastDay:lastDay+" 00:00:00";
    }

}
